package com.gitee.cui.springcloud.controller;

import java.net.URI;
import java.util.List;

import javax.annotation.Resource;

import com.gitee.cui.springcloud.entities.CommonResult;
import com.gitee.cui.springcloud.entities.Payment;
import com.gitee.cui.springcloud.lb.MyLoadBalancer;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

/**
 * 订单controller类
 *
 * @author cuiqingqiang
 * @version 1.0
 * @date 2022-03-04 17:29
 **/
@RestController
@Slf4j
@RequestMapping(value = "/consumer")
public class OrderController {

	// public static final String PAYMENT_URL = "http://localhost:8001";
	/**
	 * 通过负载均衡的方式来调用服务提供者集群，此时配置的是服务提供者的服务注册名称
	 */
	public static final String PAYMENT_URL = "http://CLOUD-PAYMENT-SERVICE";

	@Resource
	private RestTemplate restTemplate;

	@Resource
	private DiscoveryClient discoveryClient;

	@Resource
	private MyLoadBalancer myLoadBalancer;

	@GetMapping(value = "/payment/create")
	public CommonResult<Payment> create(Payment payment) {
		log.info("consumer payment: {}", payment);
		return restTemplate.postForObject(PAYMENT_URL + "/payment/create", payment, CommonResult.class);
	}

	@GetMapping(value = "/payment/get/{id}")
	public CommonResult<Payment> getPayment(@PathVariable("id") Long id) {
		log.info("consumer id: {}", id);
		return restTemplate.getForObject(PAYMENT_URL + "/payment/get/" + id, CommonResult.class);
	}

	@GetMapping(value = "/payment/createForEntity")
	public CommonResult createForEntity(Payment payment) {
		log.info("consumer payment: {}", payment);
		ResponseEntity<CommonResult> entity = restTemplate.postForEntity(PAYMENT_URL + "/payment/create", payment, CommonResult.class);
		if (entity.getStatusCode().is2xxSuccessful()) {
			return entity.getBody();
		} else {
			return new CommonResult(444, "create failed");
		}
	}

	@GetMapping(value = "/payment/getForEntity/{id}")
	public CommonResult getPaymentForEntity(@PathVariable("id") Long id) {
		log.info("consumer id: {}", id);
		ResponseEntity<CommonResult> entity = restTemplate.getForEntity(PAYMENT_URL + "/payment/get/" + id, CommonResult.class);
		if (entity.getStatusCode().is2xxSuccessful()) {
			return entity.getBody();
		} else {
			return new CommonResult(444, "query failed");
		}
	}

	@GetMapping(value = "/discovery")
	public Object discoveryClient() {
		List<String> services = discoveryClient.getServices();
		for (String service : services) {
			log.info("service: {}", service);
		}
		List<ServiceInstance> instances = discoveryClient.getInstances("CLOUD-ORDER-SERVICE");
		for (ServiceInstance instance : instances) {
			log.info("instance: serviceId: {}, Host: {}, Port: {}, URI: {}", instance.getServiceId(), instance.getHost(), instance.getPort(), instance.getUri());
		}
		return discoveryClient;
	}

	@GetMapping(value = "/payment/get/lb")
	public String getLB() {
		List<ServiceInstance> instances = discoveryClient.getInstances("CLOUD-PAYMENT-SERVICE");
		if (instances == null || instances.size() <= 0) {
			return null;
		}
		ServiceInstance instance = myLoadBalancer.getInstance(instances);
		URI uri = instance.getUri();
		return restTemplate.getForObject(uri.toString() + "/payment/get/lb", String.class);
	}

	@GetMapping(value = "/payment/zikpin")
	public String zipkin() {
		String result = restTemplate.getForObject(PAYMENT_URL + "/payment/zipkin", String.class);
		return result;
	}
}
